
package com.hs.common;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hs.HsBase;
import com.hs.utils.Utilities;
import com.hs.utils.XMLUtil;

public class AppInit extends HttpServlet
{
	//NetworkServerUtil netWork = null;
    public AppInit()
    {
		//netWork = new NetworkServerUtil(new PrintWriter(System.out));
    }

    public void init(ServletConfig config)
        throws ServletException
    {
        super.init(config);  
        loadAansiSql(getInitParameter("hs.sql.ansi").replace('%', '$'));
        //netWork.start();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
    	loadAansiSql(null);
    }

    public void loadAansiSql(String filePath)
        throws ServletException
    {
        if(filePath == null)
        	filePath = System.getProperty("hs.sql.ansi");
        if(filePath == null)
            throw new ServletException("Can't find hs conf root param!");
        System.out.println("Hs Framework 1.0.0 Starting...");
        try
        {
            String appPath = getServletContext().getRealPath("/");
            String classLoderPath = Utilities.getClassLoaderRoot().getAbsolutePath();
            System.out.println("classLoader:" + classLoderPath);
            String fullPath = classLoderPath + System.getProperty("file.separator") + filePath;
            int sqlSize = XMLUtil.parseXML2Map(fullPath);
            System.out.println("共加载SQL：" + sqlSize + "条!");
//            System.setProperty("derby.system.home","D:/preSpring/database");
//            System.out.println("derby.system.home:" + System.getProperty("derby.system.home"));
        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println("Hs Framework 1.0.0 initial fail.");
            return;
        }
        System.out.println("Hs Framework 1.0.0 Start OK.");
    }
    public void destroy(){
    	//netWork.shutdown();
    	System.out.println("应用服务停止，服务脏听");
    }

}
